/**
 * Copyright (c) Software Institute of PKU
 * All rights reserved. 
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 */
package edu.pku.sei.modeler.explorer;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.swt.graphics.Image;

import edu.pku.sei.modeler.explorer.registry.ITreeViewLabelProvider;
import edu.pku.sei.modeler.explorer.registry.TreeViewProviderRegistry;
import edu.pku.sei.modeler.images.ImageProvider;
import edu.pku.sei.modeler.model.core.MetaClass;
import edu.pku.sei.modeler.model.core.MetaComment;
import edu.pku.sei.modeler.model.core.MetaDataType;
import edu.pku.sei.modeler.model.core.MetaElement;
import edu.pku.sei.modeler.model.core.MetaEnum;
import edu.pku.sei.modeler.model.core.MetaGeneralization;
import edu.pku.sei.modeler.model.core.MetaModel;
import edu.pku.sei.modeler.model.core.MetaNamedElement;
import edu.pku.sei.modeler.model.core.MetaPackage;
import edu.pku.sei.modeler.model.core.MetaReference;
import edu.pku.sei.modeler.model.structure.MetaDiagram;
import edu.pku.sei.modeler.project.AbstractModelingProject;

/**
 * @author xy
 * @author zl
 *
 */
public class ModelViewLabelProvider extends LabelProvider {
	public String getText(Object element) {
		if (element instanceof IProject) {
			return ((IProject)element).getName();
		}
		else if(element instanceof IFolder) {
			return ((IFolder)element).getName();
		}
		else if(element instanceof IFile) {
			return ((IFile)element).getName();
		}
//		else if (element instanceof MetaNamedElement) {
//			return ( (MetaNamedElement)element ).getName();
//		}
//		else if(element instanceof MetaComment) {
//			return new String("comment");
//		}
//		else if(element instanceof MetaGeneralization) {
//			MetaGeneralization gen = (MetaGeneralization)element;
//			if(gen.getGeneral()!=null) {
//				return "base:"+gen.getGeneral().getName();
//			}
//			return "no base";
//		}
		else if(element instanceof AbstractModelingProject) {
			AbstractModelingProject proj = (AbstractModelingProject)element;
			return proj.getProject().getName();
		}
		
		else if(element instanceof MetaElement){
			ITreeViewLabelProvider labelProvider = (ITreeViewLabelProvider) TreeViewProviderRegistry.getDefault().getLabelProvider(element);
			return labelProvider.getText(element);
		}
		return new String("default");
	}
	
	public Image getImage(Object element) {
		ImageDescriptor iDesc;
		if(element instanceof IProject) {
			IProject proj = (IProject)element;
			if(proj.isOpen()) {
				iDesc = ImageProvider.getImageDescriptor(ImageProvider.PROJECT_IMG);
			}
			else {
				iDesc = ImageProvider.getImageDescriptor(ImageProvider.PROJECT_CLOSED_IMG);
			}
		}
		else if(element instanceof IFolder) {
			iDesc = ImageProvider.getImageDescriptor(ImageProvider.FOLDER_IMG);
		}
		else if(element instanceof IFile) {
			iDesc = ImageProvider.getImageDescriptor(ImageProvider.FILE_IMG);
		}
		else if (element instanceof AbstractModelingProject) {
			iDesc = ImageProvider.getImageDescriptor(ImageProvider.PROJECT);
		}
		else if (element instanceof MetaElement){
			ILabelProvider labelProvider = TreeViewProviderRegistry.getDefault().getLabelProvider(element);
			return labelProvider.getImage(element);
		}
		else {
			iDesc = ImageProvider.getImageDescriptor(ImageProvider.UNKNOWN);
		}	
		return iDesc.createImage();
	}
}